///////////////////////////////////////////////////////////////////////////////
// Projekt: O'n'X
// Autor: Lord Darkstorm
// Data utworzenia: 09.08.2013
// Opis: Deklaracja klasy GameInterface.
///////////////////////////////////////////////////////////////////////////////

#include "gameinterface.h"
#include "ui_gameinterface.h"

// Konstruktor ////////////////////////////////////////////////////////////////

GameInterface::GameInterface(QWidget *parent, short gameType, bool whoStarts) :
  QDialog(parent, Qt::WindowTitleHint),
  ui(new Ui::GameInterface)
{
  ui -> setupUi(this) ;

  // Inicjalizacja tablicy

  for (int i = 0 ; i < 3 ; i++)
  {
    for (int j = 0 ; j < 3 ; j++)
    {
      resultTable[i][j] = Unmarked ;
    }
  }

  playerTurn = whoStarts ;
  playerWon = false ;
  tie = false ;
  type = gameType ;

  connect(ui -> field1, SIGNAL(clicked() ), this, SLOT(markField1() ) ) ;
  connect(ui -> field2, SIGNAL(clicked() ), this, SLOT(markField2() ) ) ;
  connect(ui -> field3, SIGNAL(clicked() ), this, SLOT(markField3() ) ) ;
  connect(ui -> field4, SIGNAL(clicked() ), this, SLOT(markField4() ) ) ;
  connect(ui -> field5, SIGNAL(clicked() ), this, SLOT(markField5() ) ) ;
  connect(ui -> field6, SIGNAL(clicked() ), this, SLOT(markField6() ) ) ;
  connect(ui -> field7, SIGNAL(clicked() ), this, SLOT(markField7() ) ) ;
  connect(ui -> field8, SIGNAL(clicked() ), this, SLOT(markField8() ) ) ;
  connect(ui -> field9, SIGNAL(clicked() ), this, SLOT(markField9() ) ) ;
  connect(this, SIGNAL(changedTurn() ), this, SLOT(updateStatus() ) ) ;

  // Aktualizujemy pole z informacją o turze.

  if (gameType == NewGameDialog::PlayerVSPlayerLocal) emit changedTurn() ;

  // Jeśli komputer zaczyna, to wywoływana jest jego metoda.

  if (gameType == NewGameDialog::PlayerVSComputer && !playerTurn)
  {
    computerTurn() ;
  }
}

// Konstruktor ////////////////////////////////////////////////////////////////////////
// Rożnica między tym a poprzednim jest taka, że jako parametr przyjmuje dodatkowo
// deskryptor gniazda oraz jest on dostosowany do trybu gry przez sieć.

GameInterface::GameInterface(QWidget *parent, short gameType, bool whoStarts, int sID) :
  QDialog(parent, Qt::WindowTitleHint),
  ui(new Ui::GameInterface)
{
  ui -> setupUi(this) ;

  for (int i = 0 ; i < 3 ; i++)
  {
    for (int j = 0 ; j < 3 ; j++)
    {
      resultTable[i][j] = Unmarked ;
    }
  }

  playerTurn = whoStarts ;
  playerWon = false ;
  tie = false ;
  type = gameType ;
  socket = new QTcpSocket(this) ;

  socket -> setSocketDescriptor(sID) ;

  if (playerTurn)
  {
    ui -> turnLabel -> setText("Twój ruch.") ;
    ui -> turnLabel -> setStyleSheet("color: rgb(255, 0, 0)") ;
  }
  else
  {
    ui -> turnLabel -> setText("Oczekiwanie...") ;
    ui -> turnLabel -> setStyleSheet("color: rgb(0, 0, 255)") ;

    ui -> field1 -> setDisabled(true) ;
    ui -> field2 -> setDisabled(true) ;
    ui -> field3 -> setDisabled(true) ;
    ui -> field4 -> setDisabled(true) ;
    ui -> field5 -> setDisabled(true) ;
    ui -> field6 -> setDisabled(true) ;
    ui -> field7 -> setDisabled(true) ;
    ui -> field8 -> setDisabled(true) ;
    ui -> field9 -> setDisabled(true) ;
  }

  connect(ui -> field1, SIGNAL(clicked() ), this, SLOT(markField1() ) ) ;
  connect(ui -> field2, SIGNAL(clicked() ), this, SLOT(markField2() ) ) ;
  connect(ui -> field3, SIGNAL(clicked() ), this, SLOT(markField3() ) ) ;
  connect(ui -> field4, SIGNAL(clicked() ), this, SLOT(markField4() ) ) ;
  connect(ui -> field5, SIGNAL(clicked() ), this, SLOT(markField5() ) ) ;
  connect(ui -> field6, SIGNAL(clicked() ), this, SLOT(markField6() ) ) ;
  connect(ui -> field7, SIGNAL(clicked() ), this, SLOT(markField7() ) ) ;
  connect(ui -> field8, SIGNAL(clicked() ), this, SLOT(markField8() ) ) ;
  connect(ui -> field9, SIGNAL(clicked() ), this, SLOT(markField9() ) ) ;
  connect(this, SIGNAL(changedTurn() ), this, SLOT(updateStatus() ) ) ;
  connect(socket, SIGNAL(readyRead() ), this, SLOT(readData() ) ) ;
}

// Destruktor /////////////////////////////////////////////////////////////////////////

GameInterface::~GameInterface()
{
  delete ui ;
}

// result /////////////////////////////////////////////////////////////////////////////

int GameInterface::result()
{
  // Zwraca wynik gry

  return resultGame ;
}

// markField1 ////////////////////////////////////////////////////////////////////////

void GameInterface::markField1()
{
  // 1) Ustawia krzyżyk / kółko na pierwszym polu
  // 2) Zmienia turę
  // 3) Wywołuje metodę sprawdzającą, czy ktoś po tym ruchu wygrał
  // 4) Jeśli jest to gra z komputerem, to wywołuje jego metodę
  // 5) Jeśli jest to gra poprzez sieć, wysyła przeciwnikowi pakiet z numerem pola,
  // który został oznaczony.

  // Analogicznie postępują metody markField2, markField3, itd.

  if (type == NewGameDialog::PlayerVSComputer)
  {
    ui -> field1 -> setStyleSheet("color: rgb(255, 0, 0)") ;
    ui -> field1 -> setText("X") ;
    ui -> field1 -> setDisabled(true) ;

    resultTable[0][0] = Cross ;

    playerTurn = false ;

    checkIfSomeoneWon() ;

    if (!playerWon && !tie) computerTurn() ;
  }
  else if (type == NewGameDialog::PlayerVSPlayerLocal)
  {
    if (playerTurn)
    {
      ui -> field1 -> setStyleSheet("color: rgb(255, 0, 0)") ;
      ui -> field1 -> setText("X") ;
      ui -> field1 -> setDisabled(true) ;

      resultTable[0][0] = Cross ;

      playerTurn = false ;

      checkIfSomeoneWon() ;
    }
    else
    {
      ui -> field1 -> setStyleSheet("color: rgb(0, 0, 255)") ;
      ui -> field1 -> setText("O") ;
      ui -> field1 -> setDisabled(true) ;

      resultTable[0][0] = Circle ;

      playerTurn = true ;

      checkIfSomeoneWon() ;
    }

    emit changedTurn() ;
  }
  else
  {
    if (playerTurn)
    {
      ui -> field1 -> setStyleSheet("color: rgb(255, 0, 0)") ;
      ui -> field1 -> setText("X") ;
      ui -> field1 -> setDisabled(true) ;

      resultTable[0][0] = Cross ;

      playerTurn = false ;

      socket -> write("1") ;

      checkIfSomeoneWon() ;
    }
    else
    {
      ui -> field1 -> setStyleSheet("color: rgb(0, 0, 255)") ;
      ui -> field1 -> setText("O") ;
      ui -> field1 -> setDisabled(true) ;

      resultTable[0][0] = Circle ;

      playerTurn = true ;

      socket -> write("1") ;

      checkIfSomeoneWon() ;
    }

    emit changedTurn() ;
  }
}

// markField2 //////////////////////////////////////////////////////////////////////////

void GameInterface::markField2()
{
  if (type == NewGameDialog::PlayerVSComputer)
  {
    ui -> field2 -> setStyleSheet("color: rgb(255, 0, 0)") ;
    ui -> field2 -> setText("X") ;
    ui -> field2 -> setDisabled(true) ;

    resultTable[0][1] = Cross ;

    playerTurn = false ;

    checkIfSomeoneWon() ;

    if (!playerWon && !tie) computerTurn() ;
  }
  else if (type == NewGameDialog::PlayerVSPlayerLocal)
  {
    if (playerTurn)
    {
      ui -> field2 -> setStyleSheet("color: rgb(255, 0, 0)") ;
      ui -> field2 -> setText("X") ;
      ui -> field2 -> setDisabled(true) ;

      resultTable[0][1] = Cross ;

      playerTurn = false ;

      checkIfSomeoneWon() ;
    }
    else
    {
      ui -> field2 -> setStyleSheet("color: rgb(0, 0, 255)") ;
      ui -> field2 -> setText("O") ;
      ui -> field2 -> setDisabled(true) ;

      resultTable[0][1] = Circle ;

      playerTurn = true ;

      checkIfSomeoneWon() ;
    }

    emit changedTurn() ;
  }
  else
  {
    if (playerTurn)
    {
      ui -> field2 -> setStyleSheet("color: rgb(255, 0, 0)") ;
      ui -> field2 -> setText("X") ;
      ui -> field2 -> setDisabled(true) ;

      resultTable[0][1] = Cross ;

      playerTurn = false ;

      socket -> write("2") ;

      checkIfSomeoneWon() ;
    }
    else
    {
      ui -> field2 -> setStyleSheet("color: rgb(0, 0, 255)") ;
      ui -> field2 -> setText("O") ;
      ui -> field2 -> setDisabled(true) ;

      resultTable[0][1] = Circle ;

      playerTurn = true ;

      socket -> write("2") ;

      checkIfSomeoneWon() ;
    }

    emit changedTurn() ;
  }
}

// markField3 //////////////////////////////////////////////////////////////////////////

void GameInterface::markField3()
{
  if (type == NewGameDialog::PlayerVSComputer)
  {
    ui -> field3 -> setStyleSheet("color: rgb(255, 0, 0)") ;
    ui -> field3 -> setText("X") ;
    ui -> field3 -> setDisabled(true) ;

    resultTable[0][2] = Cross ;

    playerTurn = false ;

    checkIfSomeoneWon() ;

    if (!playerWon && !tie) computerTurn() ;
  }
  else if (type == NewGameDialog::PlayerVSPlayerLocal)
  {
    if (playerTurn)
    {
      ui -> field3 -> setStyleSheet("color: rgb(255, 0, 0)") ;
      ui -> field3 -> setText("X") ;
      ui -> field3 -> setDisabled(true) ;

      resultTable[0][2] = Cross ;

      playerTurn = false ;

      checkIfSomeoneWon() ;
    }
    else
    {
      ui -> field3 -> setStyleSheet("color: rgb(0, 0, 255)") ;
      ui -> field3 -> setText("O") ;
      ui -> field3 -> setDisabled(true) ;

      resultTable[0][2] = Circle ;

      playerTurn = true ;

      checkIfSomeoneWon() ;
    }

    emit changedTurn() ;
  }
  else
  {
    if (playerTurn)
    {
      ui -> field3 -> setStyleSheet("color: rgb(255, 0, 0)") ;
      ui -> field3 -> setText("X") ;
      ui -> field3 -> setDisabled(true) ;

      resultTable[0][2] = Cross ;

      playerTurn = false ;

      socket -> write("3") ;

      checkIfSomeoneWon() ;
    }
    else
    {
      ui -> field3 -> setStyleSheet("color: rgb(0, 0, 255)") ;
      ui -> field3 -> setText("O") ;
      ui -> field3 -> setDisabled(true) ;

      resultTable[0][2] = Circle ;

      playerTurn = true ;

      socket -> write("3") ;

      checkIfSomeoneWon() ;
    }

    emit changedTurn() ;
  }
}

// markField4 //////////////////////////////////////////////////////////////////////////

void GameInterface::markField4()
{
  if (type == NewGameDialog::PlayerVSComputer)
  {
    ui -> field4 -> setStyleSheet("color: rgb(255, 0, 0)") ;
    ui -> field4 -> setText("X") ;
    ui -> field4 -> setDisabled(true) ;

    resultTable[1][0] = Cross ;

    playerTurn = false ;

    checkIfSomeoneWon() ;

    if (!playerWon && !tie) computerTurn() ;
  }
  else if (type == NewGameDialog::PlayerVSPlayerLocal)
  {
    if (playerTurn)
    {
      ui -> field4 -> setStyleSheet("color: rgb(255, 0, 0)") ;
      ui -> field4 -> setText("X") ;
      ui -> field4 -> setDisabled(true) ;

      resultTable[1][0] = Cross ;

      playerTurn = false ;

      checkIfSomeoneWon() ;
    }
    else
    {
      ui -> field4 -> setStyleSheet("color: rgb(0, 0, 255)") ;
      ui -> field4 -> setText("O") ;
      ui -> field4 -> setDisabled(true) ;

      resultTable[1][0] = Circle ;

      playerTurn = true ;

      checkIfSomeoneWon() ;
    }

    emit changedTurn() ;
  }
  else
  {
    if (playerTurn)
    {
      ui -> field4 -> setStyleSheet("color: rgb(255, 0, 0)") ;
      ui -> field4 -> setText("X") ;
      ui -> field4 -> setDisabled(true) ;

      resultTable[1][0] = Cross ;

      playerTurn = false ;

      socket -> write("4") ;

      checkIfSomeoneWon() ;
    }
    else
    {
      ui -> field4 -> setStyleSheet("color: rgb(0, 0, 255)") ;
      ui -> field4 -> setText("O") ;
      ui -> field4 -> setDisabled(true) ;

      resultTable[1][0] = Circle ;

      playerTurn = true ;

      socket -> write("4") ;

      checkIfSomeoneWon() ;
    }

    emit changedTurn() ;
  }
}

// markField5 //////////////////////////////////////////////////////////////////////////

void GameInterface::markField5()
{
  if (type == NewGameDialog::PlayerVSComputer)
  {
    ui -> field5 -> setStyleSheet("color: rgb(255, 0, 0)") ;
    ui -> field5 -> setText("X") ;
    ui -> field5 -> setDisabled(true) ;

    resultTable[1][1] = Cross ;

    playerTurn = false ;

    checkIfSomeoneWon() ;

    if (!playerWon && !tie) computerTurn() ;
  }
  else if (type == NewGameDialog::PlayerVSPlayerLocal)
  {
    if (playerTurn)
    {
      ui -> field5 -> setStyleSheet("color: rgb(255, 0, 0)") ;
      ui -> field5 -> setText("X") ;
      ui -> field5 -> setDisabled(true) ;

      resultTable[1][1] = Cross ;

      playerTurn = false ;

      checkIfSomeoneWon() ;
    }
    else
    {
      ui -> field5 -> setStyleSheet("color: rgb(0, 0, 255)") ;
      ui -> field5 -> setText("O") ;
      ui -> field5 -> setDisabled(true) ;

      resultTable[1][1] = Circle ;

      playerTurn = true ;

      checkIfSomeoneWon() ;
    }

    emit changedTurn() ;
  }
  else
  {
    if (playerTurn)
    {
      ui -> field5 -> setStyleSheet("color: rgb(255, 0, 0)") ;
      ui -> field5 -> setText("X") ;
      ui -> field5 -> setDisabled(true) ;

      resultTable[1][1] = Cross ;

      playerTurn = false ;

      socket -> write("5") ;

      checkIfSomeoneWon() ;
    }
    else
    {
      ui -> field5 -> setStyleSheet("color: rgb(0, 0, 255)") ;
      ui -> field5 -> setText("O") ;
      ui -> field5 -> setDisabled(true) ;

      resultTable[1][1] = Circle ;

      playerTurn = true ;

      socket -> write("5") ;

      checkIfSomeoneWon() ;
    }

    emit changedTurn() ;
  }
}

// markField6 //////////////////////////////////////////////////////////////////////////

void GameInterface::markField6()
{
  if (type == NewGameDialog::PlayerVSComputer)
  {
    ui -> field6 -> setStyleSheet("color: rgb(255, 0, 0)") ;
    ui -> field6 -> setText("X") ;
    ui -> field6 -> setDisabled(true) ;

    resultTable[1][2] = Cross ;

    playerTurn = false ;

    checkIfSomeoneWon() ;

    if (!playerWon && !tie) computerTurn() ;
  }
  else if (type == NewGameDialog::PlayerVSPlayerLocal)
  {
    if (playerTurn)
    {
      ui -> field6 -> setStyleSheet("color: rgb(255, 0, 0)") ;
      ui -> field6 -> setText("X") ;
      ui -> field6 -> setDisabled(true) ;

      resultTable[1][2] = Cross ;

      playerTurn = false ;

      checkIfSomeoneWon() ;
    }
    else
    {
      ui -> field6 -> setStyleSheet("color: rgb(0, 0, 255)") ;
      ui -> field6 -> setText("O") ;
      ui -> field6 -> setDisabled(true) ;

      resultTable[1][2] = Circle ;

      playerTurn = true ;

      checkIfSomeoneWon() ;
    }

    emit changedTurn() ;
  }
  else
  {
    if (playerTurn)
    {
      ui -> field6 -> setStyleSheet("color: rgb(255, 0, 0)") ;
      ui -> field6 -> setText("X") ;
      ui -> field6 -> setDisabled(true) ;

      resultTable[1][2] = Cross ;

      playerTurn = false ;

      socket -> write("6") ;

      checkIfSomeoneWon() ;
    }
    else
    {
      ui -> field6 -> setStyleSheet("color: rgb(0, 0, 255)") ;
      ui -> field6 -> setText("O") ;
      ui -> field6 -> setDisabled(true) ;

      resultTable[1][2] = Circle ;

      playerTurn = true ;

      socket -> write("6") ;

      checkIfSomeoneWon() ;
    }

    emit changedTurn() ;
  }
}

// markField7 //////////////////////////////////////////////////////////////////////////

void GameInterface::markField7()
{
  if (type == NewGameDialog::PlayerVSComputer)
  {
    ui -> field7 -> setStyleSheet("color: rgb(255, 0, 0)") ;
    ui -> field7 -> setText("X") ;
    ui -> field7 -> setDisabled(true) ;

    resultTable[2][0] = Cross ;

    playerTurn = false ;

    checkIfSomeoneWon() ;

    if (!playerWon && !tie) computerTurn() ;
  }
  else if (type == NewGameDialog::PlayerVSPlayerLocal)
  {
    if (playerTurn)
    {
      ui -> field7 -> setStyleSheet("color: rgb(255, 0, 0)") ;
      ui -> field7 -> setText("X") ;
      ui -> field7 -> setDisabled(true) ;

      resultTable[2][0] = Cross ;

      playerTurn = false ;

      checkIfSomeoneWon() ;
    }
    else
    {
      ui -> field7 -> setStyleSheet("color: rgb(0, 0, 255)") ;
      ui -> field7 -> setText("O") ;
      ui -> field7 -> setDisabled(true) ;

      resultTable[2][0] = Circle ;

      playerTurn = true ;

      checkIfSomeoneWon() ;
    }

    emit changedTurn() ;
  }
  else
  {
    if (playerTurn)
    {
      ui -> field7 -> setStyleSheet("color: rgb(255, 0, 0)") ;
      ui -> field7 -> setText("X") ;
      ui -> field7 -> setDisabled(true) ;

      resultTable[2][0] = Cross ;

      playerTurn = false ;

      socket -> write("7") ;

      checkIfSomeoneWon() ;
    }
    else
    {
      ui -> field7 -> setStyleSheet("color: rgb(0, 0, 255)") ;
      ui -> field7 -> setText("O") ;
      ui -> field7 -> setDisabled(true) ;

      resultTable[2][0] = Circle ;

      playerTurn = true ;      

      socket -> write("7") ;

      checkIfSomeoneWon() ;
    }

    emit changedTurn() ;
  }
}

// markField8 //////////////////////////////////////////////////////////////////////////

void GameInterface::markField8()
{
  if (type == NewGameDialog::PlayerVSComputer)
  {
    ui -> field8 -> setStyleSheet("color: rgb(255, 0, 0)") ;
    ui -> field8 -> setText("X") ;
    ui -> field8 -> setDisabled(true) ;

    resultTable[2][1] = Cross ;

    playerTurn = false ;

    checkIfSomeoneWon() ;

    if (!playerWon && !tie) computerTurn() ;
  }
  else if (type == NewGameDialog::PlayerVSPlayerLocal)
  {
    if (playerTurn)
    {
      ui -> field8 -> setStyleSheet("color: rgb(255, 0, 0)") ;
      ui -> field8 -> setText("X") ;
      ui -> field8 -> setDisabled(true) ;

      resultTable[2][1] = Cross ;

      playerTurn = false ;

      checkIfSomeoneWon() ;
    }
    else
    {
      ui -> field8 -> setStyleSheet("color: rgb(0, 0, 255)") ;
      ui -> field8 -> setText("O") ;
      ui -> field8 -> setDisabled(true) ;

      resultTable[2][1] = Circle ;

      playerTurn = true ;

      checkIfSomeoneWon() ;
    }

    emit changedTurn() ;
  }
  else
  {
    if (playerTurn)
    {
      ui -> field8 -> setStyleSheet("color: rgb(255, 0, 0)") ;
      ui -> field8 -> setText("X") ;
      ui -> field8 -> setDisabled(true) ;

      resultTable[2][1] = Cross ;

      playerTurn = false ;

      socket -> write("8") ;

      checkIfSomeoneWon() ;
    }
    else
    {
      ui -> field8 -> setStyleSheet("color: rgb(0, 0, 255)") ;
      ui -> field8 -> setText("O") ;
      ui -> field8 -> setDisabled(true) ;

      resultTable[2][1] = Circle ;

      playerTurn = true ;

      socket -> write("8") ;

      checkIfSomeoneWon() ;
    }

    emit changedTurn() ;
  }
}

// markField9 //////////////////////////////////////////////////////////////////////////

void GameInterface::markField9()
{
  if (type == NewGameDialog::PlayerVSComputer)
  {
    ui -> field9 -> setStyleSheet("color: rgb(255, 0, 0)") ;
    ui -> field9 -> setText("X") ;
    ui -> field9 -> setDisabled(true) ;

    resultTable[2][2] = Cross ;

    playerTurn = false ;

    checkIfSomeoneWon() ;

    if (!playerWon && !tie) computerTurn() ;
  }
  else if (type == NewGameDialog::PlayerVSPlayerLocal)
  {
    if (playerTurn)
    {
      ui -> field9 -> setStyleSheet("color: rgb(255, 0, 0)") ;
      ui -> field9 -> setText("X") ;
      ui -> field9 -> setDisabled(true) ;

      resultTable[2][2] = Cross ;

      playerTurn = false ;

      checkIfSomeoneWon() ;
    }
    else
    {
      ui -> field9 -> setStyleSheet("color: rgb(0, 0, 255)") ;
      ui -> field9 -> setText("O") ;
      ui -> field9 -> setDisabled(true) ;

      resultTable[2][2] = Circle ;

      playerTurn = true ;

      checkIfSomeoneWon() ;
    }

    emit changedTurn() ;
  }
  else
  {
    if (playerTurn)
    {
      ui -> field9 -> setStyleSheet("color: rgb(255, 0, 0)") ;
      ui -> field9 -> setText("X") ;
      ui -> field9 -> setDisabled(true) ;

      resultTable[2][2] = Cross ;

      playerTurn = false ;

      socket -> write("9") ;

      checkIfSomeoneWon() ;
    }
    else
    {
      ui -> field9 -> setStyleSheet("color: rgb(0, 0, 255)") ;
      ui -> field9 -> setText("O") ;
      ui -> field9 -> setDisabled(true) ;

      resultTable[2][2] = Circle ;

      playerTurn = true ;

      socket -> write("9") ;

      checkIfSomeoneWon() ;
    }

    emit changedTurn() ;
  }
}

// checkIfSomeoneWon //////////////////////////////////////////////////////////////////////////////////

void GameInterface::checkIfSomeoneWon()
{
  // Sprawdzamy wszystkie możliwe kombinacje i czy na tych polach są krzyżyki.

  if ( (resultTable[0][0] == Cross && resultTable[0][1] == Cross && resultTable[0][2] == Cross)
       || (resultTable[1][0] == Cross && resultTable[1][1] == Cross && resultTable[1][2] == Cross)
       || (resultTable[2][0] == Cross && resultTable[2][1] == Cross && resultTable[2][2] == Cross)
       || (resultTable[0][0] == Cross && resultTable[1][0] == Cross && resultTable[2][0] == Cross)
       || (resultTable[0][1] == Cross && resultTable[1][1] == Cross && resultTable[2][1] == Cross)
       || (resultTable[0][2] == Cross && resultTable[1][2] == Cross && resultTable[2][2] == Cross)
       || (resultTable[2][0] == Cross && resultTable[2][1] == Cross && resultTable[2][2] == Cross)
       || (resultTable[0][0] == Cross && resultTable[1][1] == Cross && resultTable[2][2] == Cross)
       || (resultTable[2][0] == Cross && resultTable[1][1] == Cross && resultTable[0][2] == Cross) )
  {
    if (type == NewGameDialog::PlayerVSComputer) QMessageBox::information(this, "Wynik", "Wygrałeś.") ;
    else if (type == NewGameDialog::PlayerVSPlayerLocal) QMessageBox::information(this, "Wynik", "Zwyciężył gracz X.") ;
    else
    {
      // Poza wyświetleniem informacji o wygranej,
      // zrywamy połączenie, jeśli była to gra przez sieć.

      QMessageBox::information(this, "Wynik", "Wygrałeś.") ;
      socket -> close() ;
    }

    playerWon = true ;
    resultGame = PlayerWon ;

    this -> close() ;

    // Sprawdzamy wszystkie możliwe kombinacje i czy na tych polach są kółka.
  }
  else if ( (resultTable[0][0] == Circle && resultTable[0][1] == Circle && resultTable[0][2] == Circle)
            || (resultTable[1][0] == Circle && resultTable[1][1] == Circle && resultTable[1][2] == Circle)
            || (resultTable[2][0] == Circle && resultTable[2][1] == Circle && resultTable[2][2] == Circle)
            || (resultTable[0][0] == Circle && resultTable[1][0] == Circle && resultTable[2][0] == Circle)
            || (resultTable[0][1] == Circle && resultTable[1][1] == Circle && resultTable[2][1] == Circle)
            || (resultTable[0][2] == Circle && resultTable[1][2] == Circle && resultTable[2][2] == Circle)
            || (resultTable[2][0] == Circle && resultTable[2][1] == Circle && resultTable[2][2] == Circle)
            || (resultTable[0][0] == Circle && resultTable[1][1] == Circle && resultTable[2][2] == Circle)
            || (resultTable[2][0] == Circle && resultTable[1][1] == Circle && resultTable[0][2] == Circle) )
  {
    if (type == NewGameDialog::PlayerVSComputer) QMessageBox::information(this, "Wynik", "Przegrałeś.") ;
    else if (type == NewGameDialog::PlayerVSPlayerLocal) QMessageBox::information(this, "Wynik", "Zwyciężył gracz O.") ;
    else
    {
      QMessageBox::information(this, "Wynik", "Przegrałeś.") ;
      socket -> close() ;
    }

    resultGame = OpponentWon ;

    this -> close() ;
  }
  else if (resultTable[0][0] != Unmarked && resultTable[0][1] != Unmarked && resultTable[0][2] != Unmarked
          && resultTable[1][0] != Unmarked && resultTable[1][1] != Unmarked && resultTable[1][2] != Unmarked
          && resultTable[2][0] != Unmarked && resultTable[2][1] != Unmarked && resultTable[2][2] != Unmarked)
  {
    // Jeśli nie zostało żadnego wolnego pola a nikt nie wygrał
    // to wiadomo, jaki będzie rezultat.

    QMessageBox::information(this, "Wynik", "Remis.") ;

    resultGame = Tie ;

    if (type == NewGameDialog::PlayerVSPlayerNet) socket -> close() ;

    tie = true ;

    this -> close() ;
  }
}

// updateStatus ////////////////////////////////////////////////////////////////////////////

void GameInterface::updateStatus()
{
  // Metoda służy do aktualizacji informacji o turze gracza oraz jego przeciwnika.

  if (type == NewGameDialog::PlayerVSPlayerNet)
  {
    // Jeśli jest to gra przez sieć, to trzeba zadbać o to,
    // aby osoba czekająca na ruch przeciwnika niczego nie
    // zmajstrowała przy planszy.

    if (playerTurn)
    {
      ui -> turnLabel -> setText("Twój ruch.") ;
      ui -> turnLabel -> setStyleSheet("color: rgb(255, 0, 0)") ;

      if (resultTable[0][0] == Unmarked) ui -> field1 -> setEnabled(true) ;
      if (resultTable[0][1] == Unmarked) ui -> field2 -> setEnabled(true) ;
      if (resultTable[0][2] == Unmarked) ui -> field3 -> setEnabled(true) ;
      if (resultTable[1][0] == Unmarked) ui -> field4 -> setEnabled(true) ;
      if (resultTable[1][1] == Unmarked) ui -> field5 -> setEnabled(true) ;
      if (resultTable[1][2] == Unmarked) ui -> field6 -> setEnabled(true) ;
      if (resultTable[2][0] == Unmarked) ui -> field7 -> setEnabled(true) ;
      if (resultTable[2][1] == Unmarked) ui -> field8 -> setEnabled(true) ;
      if (resultTable[2][2] == Unmarked) ui -> field9 -> setEnabled(true) ;
    }
    else
    {
      ui -> turnLabel -> setText("Oczekiwanie...") ;
      ui -> turnLabel -> setStyleSheet("color: rgb(0, 0, 255)") ;

      if (resultTable[0][0] == Unmarked) ui -> field1 -> setDisabled(true) ;
      if (resultTable[0][1] == Unmarked) ui -> field2 -> setDisabled(true) ;
      if (resultTable[0][2] == Unmarked) ui -> field3 -> setDisabled(true) ;
      if (resultTable[1][0] == Unmarked) ui -> field4 -> setDisabled(true) ;
      if (resultTable[1][1] == Unmarked) ui -> field5 -> setDisabled(true) ;
      if (resultTable[1][2] == Unmarked) ui -> field6 -> setDisabled(true) ;
      if (resultTable[2][0] == Unmarked) ui -> field7 -> setDisabled(true) ;
      if (resultTable[2][1] == Unmarked) ui -> field8 -> setDisabled(true) ;
      if (resultTable[2][2] == Unmarked) ui -> field9 -> setDisabled(true) ;
    }
  }
  else
  {
    if (playerTurn)
    {
      ui -> turnLabel -> setText("Ruch gracza X.") ;
      ui -> turnLabel -> setStyleSheet("color: rgb(255, 0, 0)") ;
    }
    else
    {
      ui -> turnLabel -> setText("Ruch gracza O.") ;
      ui -> turnLabel -> setStyleSheet("color: rgb(0, 0, 255)") ;
    }
  }
}

// computerTurn ///////////////////////////////////////////////////////////////////////

void GameInterface::computerTurn()
{
  // Metoda służy do wyrażania ruchu komputera.

  int a ;

  while(1)
  {
    a = qrand() % 9 + 1 ;

    if (a == 1)
    {
      if (resultTable[0][0] == Unmarked)
      {
        ui -> field1 -> setStyleSheet("color: rgb(0, 0, 255)") ;
        ui -> field1 -> setText("O") ;
        ui -> field1 -> setDisabled(true) ;

        resultTable[0][0] = Circle ;

        break ;
      }
    }
    if (a == 2)
    {
      if (resultTable[0][1] == Unmarked)
      {
        ui -> field2 -> setStyleSheet("color: rgb(0, 0, 255)") ;
        ui -> field2 -> setText("O") ;
        ui -> field2 -> setDisabled(true) ;

        resultTable[0][1] = Circle ;

        break ;
      }
    }
    if (a == 3)
    {
      if (resultTable[0][2] == Unmarked)
      {
        ui -> field3 -> setStyleSheet("color: rgb(0, 0, 255)") ;
        ui -> field3 -> setText("O") ;
        ui -> field3 -> setDisabled(true) ;

        resultTable[0][2] = Circle ;

        break ;
      }
    }
    if (a == 4)
    {
      if (resultTable[1][0] == Unmarked)
      {
        ui -> field4 -> setStyleSheet("color: rgb(0, 0, 255)") ;
        ui -> field4 -> setText("O") ;
        ui -> field4 -> setDisabled(true) ;

        resultTable[1][0] = Circle ;

        break ;
      }
    }
    if (a == 5)
    {
      if (resultTable[1][1] == Unmarked)
      {
        ui -> field5 -> setStyleSheet("color: rgb(0, 0, 255)") ;
        ui -> field5 -> setText("O") ;
        ui -> field5 -> setDisabled(true) ;

        resultTable[1][1] = Circle ;

        break ;
      }
    }
    if (a == 6)
    {
      if (resultTable[1][2] == Unmarked)
      {
        ui -> field6 -> setStyleSheet("color: rgb(0, 0, 255)") ;
        ui -> field6 -> setText("O") ;
        ui -> field6 -> setDisabled(true) ;

        resultTable[1][2] = Circle ;

        break ;
      }
    }
    if (a == 7)
    {
      if (resultTable[2][0] == Unmarked)
      {
        ui -> field7 -> setStyleSheet("color: rgb(0, 0, 255)") ;
        ui -> field7 -> setText("O") ;
        ui -> field7 -> setDisabled(true) ;

        resultTable[2][0] = Circle ;

        break ;
      }
    }
    if (a == 8)
    {
      if (resultTable[2][1] == Unmarked)
      {
        ui -> field8 -> setStyleSheet("color: rgb(0, 0, 255)") ;
        ui -> field8 -> setText("O") ;
        ui -> field8 -> setDisabled(true) ;

        resultTable[2][1] = Circle ;

        break ;
      }
    }
    if (a == 9)
    {
      if (resultTable[2][2] == Unmarked)
      {
        ui -> field9 -> setStyleSheet("color: rgb(0, 0, 255)") ;
        ui -> field9 -> setText("O") ;
        ui -> field9 -> setDisabled(true) ;

        resultTable[2][2] = Circle ;

        break ;
      }
    }
  }

  // Sprawdzamy, czy ktoś wygrał.

  checkIfSomeoneWon() ;
}

// readData ////////////////////////////////////////////////////////////////////////////////////

void GameInterface::readData()
{
  // Odczytujemy numer pola z pakietu i ustawiamy odpowiednie informacje o danym polu.

  QByteArray array = socket -> readAll() ;

  int a = array.toInt() ;

  switch(a)
  {
    case 1:
      if (playerTurn)
      {
        ui -> field1 -> setText("X") ;
        ui -> field1 -> setStyleSheet("color: rgb(255, 0, 0)") ;
        ui -> field1 -> setDisabled(true) ;

        playerTurn = false ;

        resultTable[0][0] = Cross ;
      }
      else
      {
        ui -> field1 -> setText("O") ;
        ui -> field1 -> setStyleSheet("color: rgb(0, 0, 255)") ;
        ui -> field1 -> setDisabled(true) ;

        playerTurn = true ;

        resultTable[0][0] = Circle ;
      }

      break ;

    case 2:
      if (playerTurn)
      {
        ui -> field2 -> setText("X") ;
        ui -> field2 -> setStyleSheet("color: rgb(255, 0, 0)") ;
        ui -> field2 -> setDisabled(true) ;

        playerTurn = false ;

        resultTable[0][1] = Cross ;
      }
      else
      {
        ui -> field2 -> setText("O") ;
        ui -> field2 -> setStyleSheet("color: rgb(0, 0, 255)") ;
        ui -> field2 -> setDisabled(true) ;

        playerTurn = true ;

        resultTable[0][1] = Circle ;
      }

      break ;

    case 3:
      if (playerTurn)
      {
        ui -> field3 -> setText("X") ;
        ui -> field3 -> setStyleSheet("color: rgb(255, 0, 0)") ;
        ui -> field3 -> setDisabled(true) ;

        playerTurn = false ;

        resultTable[0][2] = Cross ;
      }
      else
      {
        ui -> field3 -> setText("O") ;
        ui -> field3 -> setStyleSheet("color: rgb(0, 0, 255)") ;
        ui -> field3 -> setDisabled(true) ;

        playerTurn = true ;

        resultTable[0][2] = Circle ;
      }

      break ;

    case 4:
      if (playerTurn)
      {
        ui -> field4 -> setText("X") ;
        ui -> field4 -> setStyleSheet("color: rgb(255, 0, 0)") ;
        ui -> field4 -> setDisabled(true) ;

        playerTurn = false ;

        resultTable[1][0] = Cross ;
      }
      else
      {
        ui -> field4 -> setText("O") ;
        ui -> field4 -> setStyleSheet("color: rgb(0, 0, 255)") ;
        ui -> field4 -> setDisabled(true) ;

        playerTurn = true ;

        resultTable[1][0] = Circle ;
      }

      break ;

    case 5:
      if (playerTurn)
      {
        ui -> field5 -> setText("X") ;
        ui -> field5 -> setStyleSheet("color: rgb(255, 0, 0)") ;
        ui -> field5 -> setDisabled(true) ;

        playerTurn = false ;

        resultTable[1][1] = Cross ;
      }
      else
      {
        ui -> field5 -> setText("O") ;
        ui -> field5 -> setStyleSheet("color: rgb(0, 0, 255)") ;
        ui -> field5 -> setDisabled(true) ;

        playerTurn = true ;

        resultTable[1][1] = Circle ;
      }

      break ;

    case 6:
      if (playerTurn)
      {
        ui -> field6 -> setText("X") ;
        ui -> field6 -> setStyleSheet("color: rgb(255, 0, 0)") ;
        ui -> field6 -> setDisabled(true) ;

        playerTurn = false ;

        resultTable[1][2] = Cross ;
      }
      else
      {
        ui -> field6 -> setText("O") ;
        ui -> field6 -> setStyleSheet("color: rgb(0, 0, 255)") ;
        ui -> field6 -> setDisabled(true) ;

        playerTurn = true ;

        resultTable[1][2] = Circle ;
      }

      break ;

    case 7:
      if (playerTurn)
      {
        ui -> field7 -> setText("X") ;
        ui -> field7 -> setStyleSheet("color: rgb(255, 0, 0)") ;
        ui -> field7 -> setDisabled(true) ;

        playerTurn = false ;

        resultTable[2][0] = Cross ;
      }
      else
      {
        ui -> field7 -> setText("O") ;
        ui -> field7 -> setStyleSheet("color: rgb(0, 0, 255)") ;
        ui -> field7 -> setDisabled(true) ;

        playerTurn = true ;

        resultTable[2][0] = Circle ;
      }

      break ;

    case 8:
      if (playerTurn)
      {
        ui -> field8 -> setText("X") ;
        ui -> field8 -> setStyleSheet("color: rgb(255, 0, 0)") ;
        ui -> field8 -> setDisabled(true) ;

        playerTurn = false ;

        resultTable[2][1] = Cross ;
      }
      else
      {
        ui -> field8 -> setText("O") ;
        ui -> field8 -> setStyleSheet("color: rgb(0, 0, 255)") ;
        ui -> field8 -> setDisabled(true) ;

        playerTurn = true ;

        resultTable[2][1] = Circle ;
      }

      break ;

    case 9:
      if (playerTurn)
      {
        ui -> field9 -> setText("X") ;
        ui -> field9 -> setStyleSheet("color: rgb(255, 0, 0)") ;
        ui -> field9 -> setDisabled(true) ;

        playerTurn = false ;

        resultTable[2][2] = Cross ;
      }
      else
      {
        ui -> field9 -> setText("O") ;
        ui -> field9 -> setStyleSheet("color: rgb(0, 0, 255)") ;
        ui -> field9 -> setDisabled(true) ;

        playerTurn = true ;

        resultTable[2][2] = Circle ;
      }

      break ;
  }

  checkIfSomeoneWon() ;

  emit changedTurn() ;
}
